//
//  TTHttpRequest.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/6/3.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "AFNetworking.h"
#import "TTHttpRequest.h"
#import "Utilities.h"
@implementation TTHttpRequest

- (void)getStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10.0;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
        
    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@"token"];

    [manager GET:self.url parameters:self.getData headers:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
            
            TT_HttpResponse *response = [[TT_HttpResponse alloc] init];
            response.code = [[jsonDict objectForKey:@"code"] intValue];
            response.success = [[jsonDict objectForKey:@"success"] boolValue];
            response.data = [jsonDict objectForKey:@"data"];
            response.message = [jsonDict objectForKey:@"message"];
            
            self.finishBlock(response);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            self.failedBlock(error);
        }];
}

- (void)postStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10.0;
    
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
    
    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@"token"];
    [manager POST:self.url parameters:self.postData headers:@{} progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
            
            //解决基础字段的model
            TT_HttpResponse *response = [[TT_HttpResponse alloc] init];
            response.code = [[jsonDict objectForKey:@"code"] intValue];
            response.success = [[jsonDict objectForKey:@"success"] boolValue];
            response.data = [jsonDict objectForKey:@"data"];
            response.message = [jsonDict objectForKey:@"message"];
            
            self.finishBlock(response);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            self.failedBlock(error);
        }];
}

- (void)goLoginWithMessage:(NSString *)msg {
}

@end
